/*
 * @Author: your name
 * @Date: 2020-04-09 15:03:23
 * @LastEditTime: 2020-05-08 21:00:40
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \pmclouds-web\src\pages\PersonnelFiles\common\events.js
 */
import { RenderHtml} from "./index.js";
import { Message } from "tinper-bee";
import { ButtonConfig} from "./const.js"
const {
    CardEditTableButtons,
} = ButtonConfig;

function onChange (changeObj = {}) {
    this.setState({
        ...changeObj
    })
}

function linkTo (pathname = "/card", {status = 'add', pk = '', record = {}} = {}){
    Message.destroy();
    this.props.router.push({pathname: pathname, state:arguments[1]})
}

function getSelectedDataFunc(selectedList, record, index, newData, dataType){
    if(this.state.status){//card
        //新版本的table
        // newData.map((item, dataindex)=>{
        //     if(typeof index === 'undefined' && selectedList.length){//证明是全选功能
        //         item["_checked"] = selectedList[0]['_checked']
        //         selectedList[dataindex]["index"] = dataindex
        //         selectedList[dataindex]["key"] = dataindex
        //     }
            
        //     item["index"] = dataindex
        //     item["key"] = dataindex
        // })
        //旧版本的table
        this.state.tableData.map((item, dataindex)=>{
            if(selectedList.length === 0){
                item["_checked"] =false;
            }else{
                if(dataindex === index){
                    item["_checked"] = record['_checked']
                }
                if(typeof index === 'undefined'){//证明是全选功能
                    item["_checked"] = selectedList[0]['_checked']
                    selectedList[dataindex]["index"] = dataindex
                    selectedList[dataindex]["key"] = dataindex
                }
             
            }
            
            item["index"] = dataindex
            item["key"] = dataindex
        })
       
        if(typeof index !== 'undefined'){
            for(let item of selectedList){
                if(item.pkPersonnel && item.pkPersonnel === record['pkPersonnel']){
                    item["index"] = index
                    item["key"] = index
                    break;
                }
            }
            
        }

        this.setState({
            [dataType] : [...selectedList],
            tableData : [...this.state.tableData]
        },()=>{
            if(! this.state.selectedTableData.length){
                this.props.button.setButtonDisable(["Empower","UnEmpower" ], true)
            }else{
                this.props.button.setButtonDisable(["Empower","UnEmpower" ], false)
            }
          
        })
    }else{//list
        this.setState({
            selectedTableData : [...selectedList],
        })
    }
  
   
}

function cardParams () {
    let {tableData, deleteTableS} = this.state;
    
    let headVOs = this.props.form.collectFormValue();
    let {id, item, name} = this.modalTreeC.output.getSelectDataByDataSouce("personnelFiles_modalOrgTree_form_area");

    let concatData = structorBoolean.call(this,  tableData)
    return {...headVOs , pkOrgs:id, orgNames: name, personnelVOS : concatData}
}

function structorBoolean(data){
    data = data ? data : this.state.tableData
    data.map((item, index)=>{
         if(typeof item.isContacts === 'boolean'||typeof item.isSpecialWork === 'boolean'|| typeof item.isYgcAuthorizer === 'boolean'){
             item.isContacts = item.isContacts? 1: 0;
             item.isSpecialWork = item.isSpecialWork? 1: 0;
             item.isYgcAuthorizer = item.isYgcAuthorizer? 1: 0;
         }
    })
    return  data
}

function buttonBrowseInit(){
    let buttons = {}
    if(!'企业管理员'){//
        buttons ={"Add": true, "Delete": true}
    }else{
        buttons ={"Add": false,"Delete": false}
    }
   
    if(this.state.selectedTableData.length){
        Object.assign(buttons, {"CreatePerson": false,"Empower": false, "UnEmpower": false,})
    }else{
        Object.assign(buttons, {"CreatePerson": true,"Empower": true, "UnEmpower": true})
    }
}


function addOperationCol(){
    let {
        status
    } = this.state;
    if(status !== "browse"){
        this.state.columns.push(
            { title: "操作", dataIndex: "operation", key: "operation",fixed: "right", render :(text, record, index)=>{
                if(this.state.status !=="browse"){
                    let buttons = CardEditTableButtons;
                     
                    return( RenderHtml['operationHtml'].call(this, text, record, index, Array.from(new Set(buttons))))
                }                   
                
            }}
        )
        this.setState({
            columns :  this.state.columns
        })
    }
}

export {
    onChange,
    linkTo,
    getSelectedDataFunc,
    cardParams,
    buttonBrowseInit,
    addOperationCol,
    structorBoolean
}